package cn.youyeku.zsxh;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.youyeku.zsxh.Utils.AppConfig;
import cn.youyeku.zsxh.Utils.CircleImageView;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.Utils.Tools;
import cn.youyeku.zsxh.adapter.ViewPagerAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.common.Contanst;
import cn.youyeku.zsxh.common.DoubleClickExitHelper;
import cn.youyeku.zsxh.service.MessageService;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.fragment.FgmQuestion;
import cn.youyeku.zsxh.ui.fragment.FgmLibs;
import cn.youyeku.zsxh.ui.fragment.FgmMutualHelp;
import cn.youyeku.zsxh.ui.teacher.ActTeacher;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, Toolbar.OnMenuItemClickListener, NavigationView.OnNavigationItemSelectedListener {

    private static final int REQUEST_PERMISSION = 9999;
    @BindView(R.id.main_view_page)
    ViewPager mainViePage;
    @BindView(R.id.main_tabs)
    TabLayout mainTabs;
    @BindView(R.id.side_toolbar)
    Toolbar side_toolbar;
    @BindView(R.id.nav_view)
    NavigationView mainNavigationView;
    @BindView(R.id.main_iv_head)


    CircleImageView mainIvHead;
    @BindView(R.id.tv_navhead)
    TextView tvNavhead;
    @BindView(R.id.main_appbar)
    AppBarLayout mainAppbar;

    private DoubleClickExitHelper mDoubleClickExitHelper;
    private Context mContext;
    private CircleImageView side_face;
    private TextView tv_side;


    //检查权限
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted.
            } else {
                // User refused to grant permission.
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        Tools.initEffect(getWindow());
        ButterKnife.bind(this);
        initTabsViewPage();
        side_toolbar.setOnMenuItemClickListener(this);
        mainNavigationView.setNavigationItemSelectedListener(this);
        //exit
        mDoubleClickExitHelper = new DoubleClickExitHelper();
        mContext = getApplicationContext();

        startServer();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA}, REQUEST_PERMISSION);


        }

        if(AppConfig.CONF_IS_TEACHER){
            startActivity(new Intent(getApplicationContext(),ActTeacher.class));
        }

        initView();

    }

    private void initView() {
        Glide.with(getApplicationContext())
                .load(XhApi.getFace(AppConfig.CONF_USER_ID))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.drawable.default_avatar)
                .into(mainIvHead);

        LogUtils.w(AppConfig.CONF_USER_NICK_NAME);
        tvNavhead.setText(AppConfig.CONF_USER_NICK_NAME);



        //侧边栏视图进行填充
        View sideLayout = mainNavigationView.inflateHeaderView(R.layout.act_main_header);
        side_face = (CircleImageView) sideLayout.findViewById(R.id.iv_side_faceImage);
        tv_side = (TextView) sideLayout.findViewById(R.id.navheader_nickName);

        Glide.with(getApplicationContext())
                .load(XhApi.getFace(AppConfig.CONF_USER_ID))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.drawable.default_avatar)
                .into(side_face);
        tv_side.setText(AppConfig.CONF_USER_NICK_NAME);

        side_face.setOnClickListener(this);
        mainIvHead.setOnClickListener(this);



    }


    private void initTabsViewPage() {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FgmQuestion(), "答疑中心");
        adapter.addFragment(new FgmLibs(), "题库");
        adapter.addFragment(new FgmMutualHelp(), "互助平台");

        mainViePage.setAdapter(adapter);
        assert mainTabs != null;
        mainTabs.setupWithViewPager(mainViePage);
    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {

        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_side_faceImage:
                UIControl.showUserInfo(mContext,AppConfig.CONF_USER_ID);
                break;
            case R.id.main_iv_head:
                UIControl.showUserInfo(mContext,AppConfig.CONF_USER_ID);
                break;

        }

    }

    private void startServer() {
        Intent intent = new Intent(mContext, MessageService.class);
        intent.setAction("ye");
        mContext.startService(intent);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


        return mDoubleClickExitHelper.onKeyDown(keyCode, event);
    }




    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.nav_firstPage:
                break;

            case R.id.nav_myMsg:
                UIControl.showMsgListActivity(mContext);

                break;
            case R.id.nav_personalCenter:
                UIControl.showUserInfo(mContext,AppConfig.CONF_USER_ID);
                break;
            case R.id.nav_focusedPeople:
                UIControl.showFansFollowActivity(mContext, Contanst.USER_CETER_FANS);

                break;
            case R.id.nav_havehelp:
                UIControl.showActMutualAccepted(mContext);

                break;

            case R.id.nav_settingOrhelp:
                UIControl.showSetting(mContext);

                break;
            default:
                break;
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        assert drawer != null;
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
